package plugin.exporttokens;

/*
 * AttackToken.java
 * Copyright 2003 (C) Devon Jones <soulcatcher@evilsoft.org>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.     See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
 *
 * Created on December 15, 2003, 12:21 PM
 *
 * Current Ver: $Revision: 2611 $
 * Last Editor: $Author: jdempsey $
 * Last Edited: $Date: 2007-03-29 17:27:19 -0500 (Thu, 29 Mar 2007) $
 *
 */

import pcgen.core.PlayerCharacter;
import pcgen.io.ExportHandler;
import pcgen.io.exporttoken.AttackToken;
import pcgen.util.Delta;

import java.util.StringTokenizer;

/**
 * Class Deals with:
 *
 * ATTACK.GRAPPLE.BASE
 * ATTACK.GRAPPLE.EPIC
 * ATTACK.GRAPPLE.MISC
 * ATTACK.GRAPPLE.SIZE
 * ATTACK.GRAPPLE.STAT
 * ATTACK.GRAPPLE.TOTAL
 * ATTACK.MELEE.BASE
 * ATTACK.MELEE.EPIC
 * ATTACK.MELEE.MISC
 * ATTACK.MELEE.SIZE
 * ATTACK.MELEE.STAT
 * ATTACK.MELEE.TOTAL
 * ATTACK.RANGED.BASE
 * ATTACK.RANGED.EPIC
 * ATTACK.RANGED.MISC
 * ATTACK.RANGED.SIZE
 * ATTACK.RANGED.STAT
 * ATTACK.RANGED.TOTAL
 * ATTACK.UNARMED.BASE
 * ATTACK.UNARMED.EPIC
 * ATTACK.UNARMED.SIZE
 * ATTACK.UNARMED.TOTAL
 */

public class AttackDroidPCViewerToken extends AttackToken 
{
	
	/** Token name */
	public static final String TOKENNAME = "ATTACKD";

	/**
	 * @see pcgen.io.exporttoken.Token#getTokenName()
	 */
	@Override
	public String getTokenName()
	{
		return TOKENNAME;
	}

	/**
	 * @see pcgen.io.exporttoken.Token#getToken(java.lang.String, pcgen.core.PlayerCharacter, pcgen.io.ExportHandler)
	 */
	@Override
	public String getToken(String tokenSource, PlayerCharacter pc, ExportHandler eh)
	{
		String retString = "";
		StringTokenizer aTok = new StringTokenizer(tokenSource, ".");
		aTok.nextToken();

		if (aTok.hasMoreTokens())
		{
			String attackType = aTok.nextToken();
			String modifier = aTok.hasMoreTokens() ? aTok.nextToken() : "";

			retString = getParsedTokenEx(pc, attackType, modifier);
		}

		return retString;
	}

	/**
	 * Translate an already parsed attack token for the supplied character.
	 * This will return the attack token value requested for the supplied
	 * character. Note the return value is always a formatted string such as +8
	 * or +8/+3
	 *
	 * @param pc The character to retrieve the attack value for.
	 * @param attackType The type of attack to the returned
	 *                    - GRAPPLE, MELLEE, RANGED or UNARMED
	 * @param modifier The modified to the attack - BASEDAMAGE, BONUSDAMAGE, FINESSABLE or an empty string
	 * @return The token value.
	 */
	private String getParsedTokenEx(PlayerCharacter pc, String attackType, String modifier)
	{
		if (attackType.equals("UNARMED"))
		{
			if (modifier.equals("BASEDAMAGE"))
				return getUnarmedDamageBaseToken(pc);
			
			if (modifier.equals("BONUSDAMAGE"))
				return getUnarmedDamageBonusToken(pc);
			
			if (modifier.equals("FINESSEABLE"))
				return String.valueOf(getFinessableToken(pc));
		}
		
		return String.valueOf(0);
	}

	/**
	 * Get total FINESSEABLE token
	 * @param pc
	 * @param aType
	 * @return total FINESSEABLE token
	 */
	private int getFinessableToken(PlayerCharacter pc)
	{
		int totalBonus = (int)pc.getTotalBonusTo("COMBAT", "TOHIT.Finesseable");
		return (totalBonus > 0 ? 1 : 0);
	}

	/**
	 * Get the DAMAGEBASE token
	 * @param pc
	 * @return DAMAGEBASE token
	 */
	private String getUnarmedDamageBaseToken(PlayerCharacter pc)
	{
		return pc.getUnarmedDamageString(false, false);
	}

	/**
	 * Get the DAMAGEBONUS token
	 * @param pc
	 * @return DAMAGEBONUS token
	 */
	private String getUnarmedDamageBonusToken(PlayerCharacter pc)
	{
		return Delta.toString(0);
	}
}
